package databaseSharding;

import java.util.List;

public class MyDatabaseShardingStrategy implements DatabaseShardingStrategy {
    private final List<String> databaseNames;
    private final List<String> tableNames;

    public MyDatabaseShardingStrategy(List<String> databaseNames, List<String> tableNames) {
        this.databaseNames = databaseNames;
        this.tableNames = tableNames;
    }

    @Override
    public String getDatabaseName(String key) {
        // 根据key取模来选择数据库
        int index = Math.abs(key.hashCode()) % databaseNames.size();
        return databaseNames.get(index);
    }

    @Override
    public String getTableName(String key) {
        // 根据key取模来选择表
        int index = Math.abs(key.hashCode()) % tableNames.size();
        return tableNames.get(index);
    }
}
